package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentMongoService;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.search.vo.StatsResult;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService  strategyDetailService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;

    @Reference//评论
    private IStrategyCommentMongoService strategyCommentMongoService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model){
        //推荐的攻略
        model.addAttribute("commends",strategyCommendService.listTop5());
        //upCds  飙升排行版
        model.addAttribute("upCds",strategyStatsCacheService.listCommends(RedisKey.STRATEGY_STATS_UP_SORT));
        //hotCds  热门排行版
        model.addAttribute("hotCds",strategyStatsCacheService.listCommends(RedisKey.STRATEGY_STATS_HOT_SORT));
        // UNatroad国外查询条件
        List<StatsResult> unabroads = strategySearchService.listCondition(SearchQueryObject.CONDITION_ABROAD);
        List<StatsResult> abroads = strategySearchService.listCondition(SearchQueryObject.CONDITION_UNABROAD);
        model.addAttribute("abroads",abroads);
        /// /是abroads 国内查询条件
        unabroads.remove(StatsResult.CHINA_Id);
        model.addAttribute("unabroads",unabroads);
        //themes 省外
        model.addAttribute("themes",strategySearchService.listCondition(SearchQueryObject.CONDITION_THEME));
        //themeCds 主题推荐
        model.addAttribute("themeCds",strategySearchService.listThemeCommendTop10());
        return "strategy/index";
    }


    @Value("${num}")  //访问数据 增加数
    private Integer num;
    @RequestMapping("detail")
    public  String detail(Long id, Model model){
        //表示该文章被访问 访问量+1
        strategyStatsCacheService.incrNum(id,num,1);

        model.addAttribute("detail",strategyDetailService.selectByPrimaryKey(id));
        //查询改文章的热数据
        model.addAttribute("vo",strategyStatsCacheService.getStrategyStats(id));
        //飙升榜单加分
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT,1,id);

        return "strategy/detail";
    }
    //查询全部的目的地 攻略
    @RequestMapping("list")
    public  String strategy(@ModelAttribute("qo")DestinationQueryObject qo, Model model){
        //tosats
        List<Destination> tosats = destinationService.listToasts(qo.getDestId());
        //catalogs
        model.addAttribute("tags", strategyCatalogService.listByDestID(qo.getDestId()));
        model.addAttribute("pageInfo",strategyDetailService.queryForList(qo));
        //dest{name:xx}
        model.addAttribute("toasts",tosats);
        model.addAttribute("dest",tosats.remove(tosats.size()-1));
        return "strategy/list";
    }

    //评论攻略
    @RequestMapping("commentAdd")
    @RequiredLogin//判断是否登入
    @ResponseBody
    public  Object commentAdd(StrategyComment entity, @LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        //填充用户数据
        entity.setUserId(user.getId());
        entity.setUsername(user.getNickname());
        entity.setHeadUrl(user.getHeadImgUrl());
        entity.setLevel(user.getLevel());
        //该评论+1
        strategyStatsCacheService.incrNum(entity.getDetailId(),1,
                strategyStatsCacheService.STRATEGY_STATS_REPLYNUM);
        //保存评论
        strategyCommentMongoService.save(entity);
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(entity.getDetailId());
        //飙升榜单加分
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT,1,entity.getDetailId());
        return result.addData(vo.getReplynum());
    }
    //评论攻略
    @RequestMapping("comment")
    public  String comment(@ModelAttribute("qo") StrategyCommentQueryObject qo ,Model model){
        //共享到页面
        model.addAttribute("page",strategyCommentMongoService.query(qo));
        return "strategy/commentTpl";
    }
    //评论攻略
    @RequestMapping("commentThumbUp")
    @ResponseBody
    @RequiredLogin
    public  Object commentThumbUp(String toId,Long fromId){
        strategyCommentMongoService.commentThumbUp(toId,fromId);
        return new AjaxResult();
    }

    @RequiredLogin
    @RequestMapping("favor")
    @ResponseBody//收藏数
    public Object favor(Long strategyId,@LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        if(user==null){
            result.mark("请先登入");
            result.setCode(102);//表示没有登入的错误状态码
            return result;
        }
        boolean flag=strategyStatsCacheService.favor(strategyId,user.getId());
        result.setSuccess(flag);
        //查询改攻略的统计数
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(strategyId);
        result.addData(vo);
        //判断加分减分
        num=flag?1:-1;
        //飙升榜单加分
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT,num,strategyId);
        return result;
    }
    @RequiredLogin
    @RequestMapping("strategyThumbup")
    @ResponseBody//点赞数
    public Object strategyThumbup(Long strategyId,@LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        if(user==null){
            result.mark("请先登入");
            result.setCode(102);//表示没有登入的错误状态码
            return result;
        }
        boolean flag=strategyStatsCacheService.strategyThumbup(strategyId,user.getId());
        result.setSuccess(flag);
        //查询改攻略的统计数
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(strategyId);
        result.addData(vo);
        //加分
        if(flag){
            //飙升榜单加分
            strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT,1,strategyId);
        }
        return result;
    }

    @RequestMapping("searchPage")
    public  String searchPage(@ModelAttribute("qo") SearchQueryObject qo ,Model model){
        //page共享到页面
        model.addAttribute("page",strategySearchService.queryForList(qo));
        return "strategy/searchPageTpl";
    }
}
